package com.buss.core.model;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonDeserialize;

import com.buss.base.model.Branch;

/**
 * 用户角色信息
 * @author gykl
 *
 */
@Entity
@Table(name="core_role")
@TableGenerator(name="role_pk_sequence",initialValue=1,allocationSize=1,pkColumnName="seq_col",
		pkColumnValue="role_seq",valueColumnName="seq_val",table="core_pk_sequence")
@JsonIgnoreProperties(value={"sysmenus","users","branchs"})
public class Role implements Serializable{

	public Role(){}
	
	public Role(Long id){
		this.id = id;
	}
	
	private static final long serialVersionUID = 2631771448237735846L;

	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator="role_pk_sequence")
	private Long id;
	
	/**
	 * 单位编码
	 */
	@Column(length = 50,nullable=false)
	private String companyId;
	
	/**
	 * 角色名称
	 */
	@Column(length = 50,nullable=false)
	private String name;
	
	/**角色简介*/
	@Column(length = 50)
	private String remark;	
	
	/**是否选择*/
	@Transient
	private Boolean checked;
	
	/**
	 * 角色菜单权限信息
	 */
	@JsonDeserialize
	@ManyToMany(cascade = CascadeType.REFRESH,fetch=FetchType.LAZY)
	@JoinTable(name="core_role_menu",inverseJoinColumns={@JoinColumn(name="menuId")},
			joinColumns={@JoinColumn(name="roleId") })
	private List<Sysmenu> sysmenus;
	
	
	/**
	 * 角色下的用户信息
	 */
	@JsonDeserialize
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
	@JoinTable(name="core_role_user",inverseJoinColumns=@JoinColumn(name="userId"),
			joinColumns=@JoinColumn(name="roleId"))
	private List<User> users;
	
	/**
	 * 角色下的店铺信息
	 */
	@JsonDeserialize
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
	@JoinTable(name="base_role_branch",inverseJoinColumns=@JoinColumn(name="branchId"),
			joinColumns=@JoinColumn(name="roleId"))
	private List<Branch> branchs;
	
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public List<Sysmenu> getSysmenus() {
		return sysmenus;
	}

	public void setSysmenus(List<Sysmenu> sysmenus) {
		this.sysmenus = sysmenus;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}	

	public List<Branch> getBranchs() {
		return branchs;
	}

	public void setBranchs(List<Branch> branchs) {
		this.branchs = branchs;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public static void main(String[] args){
		Role role = new Role();
		JsonGenerator jsongenerator = null;
		ObjectMapper mapper = new ObjectMapper();
		
		try{
			jsongenerator = mapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
			jsongenerator.writeObject(role);
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	
}
